using namespace std;

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
};


class Solution {
public:
    int Sum(TreeNode* root, int sum)
    {
        if (root == nullptr)
        {
            return 0;
        }
        sum = (sum << 1) + root->val; // +的运算符优先级高于移位
        if(root->left == nullptr && root->right == nullptr)
        {
            return sum;
        }
        else
        {
            return Sum(root->left, sum) + Sum(root->right, sum);
        }
    }
    int sumRootToLeaf(TreeNode* root) {
        return Sum(root, 0);
    }
};

